<?php

/**
 * @author gbfun <gbfun@163.com>  2018-06-18 17:37
 * http://www.wuweiyou.com
 */

class db_model_cart
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_cart::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_cart::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {

    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {

    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
    
        if(!empty($row)){
            $this->set_row($row);
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
    
        if(!empty($row)){
            $this->set_row($row);
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
    
        $this->set_rows($rows);
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);

        $this->set_rows($list['list']);
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }
  
    public function add($cart_row_options)
    {
        $this->set_row_user($cart_row_options);
    
        $add_result = $this->get_db_table()->add($cart_row_options);
    
        return $add_result;
    }
    
    public function update_good_number($cart_row_options)
    {
        $update_good_number_result = $this->get_db_table()->update_good_number($cart_row_options);
    
        return $update_good_number_result;
    }
    
    public function get_cart_list_info($member_id)
    {
        $cart_list_info = array('cart_list' => array(), 'total' => 0);
    
        $where = array('member_id' => $member_id);
        $list_temp  = $this->fetch_all($where);
        //var_dump($list_temp); exit();
        
        //清除已下架的商品规格
        $list_temp = $this->clear_cart_list_deleted_spec($list_temp);
    
        foreach ($list_temp as $item_temp) {
            $item = $item_temp;
            $item['total'] = number_format($item_temp['good_price'] * $item_temp['good_number'], 2, '.', '');
            //$item['total'] = $item_temp['good_price'] * $item_temp['good_number'];
            $cart_list_info['cart_list'][$item_temp['id']] = $item;
             
            $cart_list_info['total'] += $item['total'];
        }
    
        $cart_list_info['total'] = number_format($cart_list_info['total'], 2, '.', '');
    
        return $cart_list_info;
    }
    
    public function check_cart_list_good_spec($member_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
        
        $where = array('member_id' => $member_id);
        $cart_list  = $this->fetch_all($where);
    
        foreach ($cart_list as $key => $item) {
            $id = $item['id'];
            $good_id = $item['good_id'];
            $spec_id = $item['spec_id'];
            
            $check_good_spec_result = $this->check_good_spec($good_id, $spec_id);
            if($check_good_spec_result['status'] < 0){
                $result['status'] = $check_good_spec_result['status'];
                $result['message'] = $check_good_spec_result['message'];
                $result['data']['cart_id'] = $id;
                $result['data']['good_id'] = $good_id;
                $result['data']['spec_id'] = $spec_id;
                
                break;
            }            
        }
        
        return $result;
    }
    
    
    public function clear_cart_list_deleted_spec($cart_list)
    {       
        foreach ($cart_list as $key => $item) {
            $id = $item['id'];
            $good_id = $item['good_id'];
            $spec_id = $item['spec_id'];
            
            $check_good_spec_result = $this->check_good_spec($good_id, $spec_id);
            if($check_good_spec_result['status'] < 0){
                $where = array( 'ids' => array($id));
                $delete_result = $this->delete($where);
                
                unset($cart_list[$key]);    
            }            
        }
    
        return $cart_list;
    }
    
    public function check_good_spec($good_id, $spec_id, $if_spec_item_simple = true)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
               
        $model_foreground_good = wwy_resource_container::get_model_foreground_good();
        $good_status = 1;
        $good_info = $model_foreground_good->find($good_id, $good_status);
        if(empty($good_info)){
            $result['status']  = -4;
            $result['message'] = '不存在该商品，或该商品已修改、下架、售罄';
        }else{
            $result['data']['good_info'] = $good_info;
        }
        	
        if($result['status'] == 0){
            //规格
            if($if_spec_item_simple){
                $spec_item = $model_foreground_good->get_spec_item_simple($good_id, $spec_id);
            }else{
                $spec_item = $model_foreground_good->get_spec_item($good_id, $spec_id);
            }
            
            if(empty($spec_item)){
                $result['status']  = -5;
                $result['message'] = '不存在该商品规格，或该商品规格已修改、下架、售罄';
            }else{
                $result['data']['spec_item'] = $spec_item;
            }
        }
        
        return $result;
    }
    
    public function delete(array $ids)
    {  
        $delete_result = $this->get_db_table()->delete($ids);
    
        return $delete_result;
    }
    
    public function clear_cart($member_id)
    {
        $clear_cart_result = $this->get_db_table()->clear_cart($member_id);
        
        return $clear_cart_result;
    }
}

